package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.DataItemResult;
import com.xuexiang.yoyo.entity.MeterStatus;

import java.util.ArrayList;
import java.util.List;

public class MeterStatusMapper {
    public static final String TABLE = "meter_status";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+ TABLE +"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "code INTEGER ," +
            "content TEXT ," +
            "device_code TEXT," +
            "ln TEXT," +
            "username TEXT," +
            "attribute_index INTEGER," +
            "gxdlms_class_name TEXT);";


    public static void delete(String device_code){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"'",null);
    }
    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }

    private static List<String> GetAlarmFaultInfo(int alarmGroup, Long alarmCode) {
        List<String> AlarmFaultInfo = new ArrayList<>();
        long mask = 0x00L;
        for (int i = 0; i < 32; i++) {
            mask = 0x01L << i;
            if ((alarmCode & (int) mask) > 0) {
                AlarmFaultInfo.add(ALARMS[alarmGroup][i]);
            }
        }
        return AlarmFaultInfo;
    }

    public static void insert(DataItemResult item) {
        int alarmGroup = 0;
        if ("0.0.97.97.0.255".equals(item.getLn())) {
            alarmGroup = 0;
        } else if ("0.0.97.97.1.255".equals(item.getLn())) {
            alarmGroup = 1;
        }
        Long alarmCode = Long.valueOf( item.getValue() );
        List<String> status = GetAlarmFaultInfo(alarmGroup, alarmCode);
        String content = String.join(",", status);
        MeterStatus meterStatus = new MeterStatus();
        meterStatus.setCode(item.getValue());
        meterStatus.setContent(content);
        meterStatus.setDeviceCode(item.getInsertDeviceCode());
        meterStatus.setLn(item.getLn());
        meterStatus.setAttributeIndex(item.getAttributeIndex());
        meterStatus.setGxdlmsClassName(item.getGxdlmsClassName());
        MeterStatusMapper.insert(meterStatus);
    }

    public static void insert(MeterStatus item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("code", item.getCode());
        contentValues.put("content", item.getContent());
        contentValues.put("device_code", item.getDeviceCode());
        contentValues.put("ln", item.getLn());
        contentValues.put("username", HesAppStatic.getUsername());
        contentValues.put("attribute_index", item.getAttributeIndex());
        contentValues.put("gxdlms_class_name", item.getGxdlmsClassName());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }
    public static List<MeterStatus> queryByDeviceCode(String deviceCode){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'"+
                " and username='" + HesAppStatic.getUsername() + "'");
        List<MeterStatus> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            MeterStatus item = new MeterStatus();
            item.setId(cursor.getInt(0));
            item.setCode(cursor.getString(1));
            item.setContent(cursor.getString(2));
            item.setDeviceCode(cursor.getString(3));
            item.setLn(cursor.getString(4));
            item.setUsername(cursor.getString(5));
            item.setAttributeIndex(cursor.getInt(6));
            item.setGxdlmsClassName(cursor.getString(7));
            resultList.add(item);

        }
        cursor.close();
        return resultList;
    }



    private static final String[][] ALARMS = new String[][]{
            {
                    "Clock Fault",
                    "Low Battery",
                    "High Temperature",
                    "Reserved",
                    "Reserved",
                    "Relay Control Failed",
                    "Reserved",
                    "Reserved",
                    "ROM/FLASH Fault",
                    "RAM Fault",
                    "EEPROM Fault",
                    "Metering Fault",
                    "Watchdog Reset",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved",
                    "Reserved"
            },
            {
                    "Open Meter Cover",     //（开表盖）
                    "Open Tail Cover",     //（开端盖）
                    "Strong Magnetic Interference",     //（强磁干扰）
                    "Relay Disconnected",     //（继电器断开）
                    "Meter Fault",     //（电表错误）
                    "module removed",     //（模块盖移除）
                    "meter box opened",     //（开表箱）
                    "Three-Phase Current Reverse",     //（三相电流反向）
                    "Phase A Current Reverse",     //（A相电流反向）
                    "Phase B Current Reverse",     //（B相电流反向）
                    "Phase C Current Reverse",     //（C相电流反向）
                    "Phase A Overcurrent",     //（A相过流）
                    "Phase B Overcurrent",     //（B相过流）
                    "Phase C Overcurrent",     //（C相过流）
                    "Power Overload",     //（功率过载）
                    "Bypass",     //（旁路）
                    "Phase A Overvoltage",     //（A相过压）
                    "Phase B Overvoltage",     //（B相过压）
                    "Phase C Overvoltage",     //（C相过压）
                    "Phase A Voltage Loss",     //（A相失压）
                    "Phase B Voltage Loss",     //（B相失压）
                    "Phase C Voltage Loss",     //（C相失压）
                    "Neutral Broken",     //（零线断开）
                    "Phase Reverse",     //（相反向）
                    "Phase A Loss",     //（A相失相）
                    "Phase B Loss",     //（B相失相）
                    "Phase C Loss",     //（C相失相）
                    "Power Unbalance",     //（功率不平衡）
                    "Voltage Unbalance",     //（电压不平衡）
                    "Current Unbalance",     //（电流不平衡）
                    "Power Factor Low",     //（功率因素低）
                    "Reserve"      //（预留）
            }
    };

}
